package Properties;

import org.Utils.ReadResourceFile;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.Properties;

public class TestProperties {
    public static void main(String[] args) throws IOException {
        readFileStream();
        readMem();
    }

    private static void readFileStream() throws IOException {
        String f = "settings.properties";
        String filePath = ReadResourceFile.getPath(f);

        Properties props = new Properties();
        assert filePath != null;
        props.load(new java.io.FileInputStream(filePath));
//        props.load(TestProperties.class.getResourceAsStream(f));

        String filepath = props.getProperty("last_open_file");
        String interval = props.getProperty("auto_save_interval");

        System.out.println(filepath + " ::: " + interval);

        System.out.println("----------------------------------------");

        saveProperties();
        System.out.println("----------------------------------------");
    }

    private static void readMem() throws IOException {
        String settings = "# test" + "\n" + "course=Java" + "\n" + "last_open_date=2019-08-07T12:35:01";
        ByteArrayInputStream input = new ByteArrayInputStream(settings.getBytes("UTF-8"));
        Properties props = new Properties();
        props.load(input);

        System.out.println("course: " + props.getProperty("course"));
        System.out.println("last_open_date: " + props.getProperty("last_open_date"));
        System.out.println("last_open_file: " + props.getProperty("last_open_file", "no exist open file"));
        System.out.println("auto_save: " + props.getProperty("auto_save", "60"));

        System.out.println("----------------------------------------");
    }

    private static void saveProperties() throws IOException {
        String filePath = ReadResourceFile.getPath("local.properties");
        Properties props = new Properties();
        props.setProperty("url", "http://www.liaoxuefeng.com");
        props.setProperty("language", "Java");
        props.store(new FileOutputStream(filePath), "save properties");
    }
}
